package org.gavin.sharding.listener;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.spring.boot.datasource.DataSourceMapSetter;
import org.gavin.sharding.controller.UserController;
import org.gavin.sharding.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.Map;

/**
 * ShardingTablesLoadRunner
 *
 * @author grl
 * @date 2024/2/4
 */
@Slf4j
@Order(1)
@Component
public class ShardingTablesLoadRunner implements CommandLineRunner {

    @Autowired
    private TUserService userService;
    @Autowired
    private Environment environment;

    @Override
    public void run(String... args) {

        Map<String, DataSource> dataSourceMap = DataSourceMapSetter.getDataSourceMap(environment);
        // 读取已有分表，进行缓存
        userService.initTable();
        log.info(">>>>>>>>>> 【ShardingTablesLoadRunner】缓存已有分表成功 <<<<<<<<<<");
    }

}
